package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.WlCommodity;
import service.WlCommodityService;
import service.WlUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.util.List;

@Controller
public class WlCommodityController {
    @Resource(name="wlCommodityService")
    WlCommodityService wlCommodityService;

    public WlCommodityService getWlCommodityService() {
        return wlCommodityService;
    }

    public void setWlCommodityService(WlCommodityService wlCommodityService) {
        this.wlCommodityService = wlCommodityService;
    }
    @RequestMapping("getAll")
    public String getAll(HttpServletRequest request, Integer page){
        if(page==null){
            page=1;
        }
        int rows = wlCommodityService.getCount();

        int pageYs = rows%7==0?rows/7:rows/7+1;

        if(page>pageYs){
            page=pageYs;
            request.getSession().setAttribute("page", page);
        }else{
            if(page<=0){
                page=1;
            }
            request.getSession().setAttribute("page", page);
        }
        page = (page-1)*7;
        List<WlCommodity> w = wlCommodityService.getAll(page);
        if(w.size()>0){
            request.getSession().setAttribute("ddd", w);
            request.getSession().setAttribute("pageYs", pageYs);
        }
        return "redirect:/graph_echarts.jsp";
    }
    @RequestMapping("findOne")
    public String findOne(HttpServletRequest request,int id){
        WlCommodity air = wlCommodityService.findOne(id);
        if(air!=null){
            request.getSession().setAttribute("air", air);
            return "redirect:/upd.jsp";
        }else{
            return "/graph_echarts.jsp";
        }
    }
    @RequestMapping("del")
    public String del(int id){
        wlCommodityService.del(id);
        return "getAll";
    }
    @RequestMapping("add")
    public String add(WlCommodity air) throws UnsupportedEncodingException {
//        String jcz = new String(air.getMonitoring_station().getBytes("ISO-8859-1"),"UTF-8");
//        air.setMonitoring_station(jcz);
//        air.setLast_modify_time(new Date(new java.util.Date().getTime()).toLocaleString());
//        int result = airservice.addAir(air);
//        if(result>0){
//            return "getAll";
//        }else{
//            return "redirect:/add.jsp";
//        }
        return "";
    }
    @RequestMapping("upd")
    public String upd(WlCommodity air) throws UnsupportedEncodingException{
//        System.out.print("111");
//        String jcz = new String(air.getMonitoring_station().getBytes("ISO-8859-1"),"UTF-8");
//        air.setMonitoring_station(jcz);
//        air.setLast_modify_time(new Date(new java.util.Date().getTime()).toLocaleString());
//        wlCommodityService.upd(air);
        return "getAll";
    }
}
